#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-11-16 01:38:00
# @Author  : guoben (guoben.ly@qq.com)
# @Link    : https://git.oschina.net/guoben

import urllib
import urllib2
import re

page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent': user_agent}

try:
    request = urllib2.Request(url, headers=headers)
    response = urllib2.urlopen(request)
    content = response.read().decode('utf-8')
    pattern = re.compile('<img.*?src="(.*?)".*?alt="(.*?)"/>', re.S)
    items = re.findall(pattern, content)
    for item in items:
    	print item
    	print 
except urllib2.URLError, e:
    if hasattr(e, "code"):
        print e.code
    if hasattr(e, "reason"):
        print e.reason
